#ifndef __plots_h_
#define __plots_h_
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))

int SIZEX, SIZEY;
void plOpen(int X, int Y)
{
    SIZEX=X; SIZEY=Y;
    initwindow(X+20, Y+20, "Plot");
    setbkcolor(15);
    cleardevice();
    setcolor(0);
    line(10,10,10,Y+10);
    line(10,10,7,15);
    line(10,10,13,15);
    line(10,Y+10,X+10,Y+10);
    line(X+10,Y+10,X+5,Y+7);
    line(X+10,Y+10,X+5,Y+13);
}
void plClose()
{
    // nothing to do :) maybe just now
}
void plot(float func(float), float Xmin, float Xmax, int color=RED)
{
    float dx=(Xmax-Xmin)/SIZEX, y0=func(Xmin), y1=func(Xmin), t;
    char bu[10];
    for(int i=1; i<=SIZEX; i++)
    {
        t = func(Xmin+i*dx);
        y0=min(t,y0);
        y1=max(t,y1);
    }
    float dy=(y1-y0)/(SIZEY-3);
    curr_pt=start_pt;
    if ((y0<=-0) and (y1>=0))
    {
        for(int i=0; i<=SIZEX/7; i++)
            if (!(i%2)) line(10+7*i,13+y1/dy,7*i+17,13+y1/dy);
        outtextxy(13,16+y1/dy,itoa(0,bu,10));
    }

    moveto(10,13+(y1-func(Xmin))/dy);
    setcolor(color);
    for(int i=0; i<=SIZEX; i++)
    {
        lineto(10+i, 13+(y1-func(Xmin+i*dx))/dy);
        moveto(10+i, 13+(y1-func(Xmin+i*dx))/dy);
    }
}
float plotty(float t)
{
    while (curr_pt.t<=t)
    {
        curr_pt=RK4(curr_pt,func);
    }
    return curr_pt.x;
}
#endif
